<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form inline>
        <el-form-item label="订单号：" class="filter-item">
          <el-input placeholder="请输入订单号" size="medium" v-model="orderNo"></el-input>
        </el-form-item>
         <el-form-item label="买家手机号：" class="filter-item">
          <el-input placeholder="请输入订单号" size="medium" v-model="mobile"></el-input>
        </el-form-item>
        <el-form-item label="邀请者手机号：" class="filter-item">
          <el-input placeholder="请输入订单号" size="medium" v-model="inviteMobile"></el-input>
        </el-form-item>
        <el-form-item label="订单状态：" class="filter-item">
          <el-select v-model="status" size="medium">
            <el-option v-for="(item, index) in statusList" :key="index" :label="item.name" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="filter-item right">
          <el-button icon="el-icon-search" plain type="primary" @click="search" size="medium">搜索</el-button>
          <el-button icon="el-icon-setting" plain type="primary" @click="resetSearch" size="medium">重置</el-button>
        </el-form-item>
      </el-form>
      <el-form inline>
        <el-form-item label="售后订单导出：" class="filter-item">
           <el-date-picker
            value-format="timestamp"
            v-model="timeList"
            type="daterange"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['00:00:00', '23:59:59']">
          </el-date-picker>
        </el-form-item>
        <el-form-item class="filter-item">
          <el-button icon="el-icon-download" plain type="primary" size="medium" @click="clickToExportOrder">导出订单</el-button>
        </el-form-item>
      </el-form>
    </div>
    <table-list :data="orderList">
      <template slot="columns">
        <el-table-column fixed="left" label="订单号" align="center" width="230px">
          <template slot-scope="scope">
            <div>订单号：{{ scope.row.orderNo }}</div>
            <div>取消时间：{{ scope.row.cancelTime }}</div>
          </template>
        </el-table-column>
        <el-table-column label="订单信息" align="center" min-width="300px">
          <template slot-scope="scope">
            <div class="flex flex-item--center" v-for="(item, index) in scope.row.ordersProductsList" :key="index">
              <el-popover
                placement="top-start"
                trigger="hover">
                <img :src="item.productsCoverCn" class="previewImage">
                <img :src="item.productsCoverCn" class="thumbnail" slot="reference">
              </el-popover>
              <div class="block-right">
                <div>{{item.productsNameCn}}</div>
                <div>{{item.skuDescCn | specFilter }}</div>
                <div>数量：{{item.count}}</div>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="成交金额" align="center" min-width="100px">
          <template slot-scope="scope">
            {{ scope.row.totalPrice | divideFilter}}
          </template>
        </el-table-column>
        <el-table-column label="退款金额" align="center" min-width="100px">
          <template slot-scope="scope">
            {{ scope.row.refundAmount | divideFilter }}
          </template>
        </el-table-column>
        <el-table-column label="买家" align="center" min-width="120px">
          <template slot-scope="scope">
            {{ scope.row.mobile }}
          </template>
        </el-table-column>
        <el-table-column label="邀请者" align="center" min-width="120px">
          <template slot-scope="scope">
            {{ scope.row.inviteMobile }}
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" min-width="100px">
          <template slot-scope="scope">
            {{ scope.row.status | statusFilter}}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" align="center" width="150px">
          <template slot-scope="scope">
            <div><el-button plain type="primary" size="mini" @click="clickToOrderDetail(scope.row)">查看详情</el-button></div>
            <div style="margin-top:5px"><el-button plain type="warning" size="mini" @click="clickToResolveRefund(scope.row)" v-if="scope.row.status == 5">完成退款</el-button></div>
            <div style="margin-top:5px"><el-button plain type="warning" size="mini" @click="clickToRejectRefund(scope.row)" v-if="scope.row.status == 5">退款失败</el-button></div>
          </template>
        </el-table-column>
      </template>
    </table-list>
    <list-pagination :total="total" :page="page" :pageSize="pageSize" :handleSizeChange="handleSizeChange" :handleCurrentChange="handleCurrentChange"></list-pagination>
    <sale-order-dialog :dialogVisible="saleOrderDialogVisible" @closeDialog="saleOrderDialogVisible = false" :dialogData="orderDetail"></sale-order-dialog>
  </div>
</template>

<script>
import list from '@/mixins/List'
import table from '@/components/Table'
import listPagination from '@/components/ListPagination'
import saleOrderDialog from './components/saleOrderDialog'
import { getSaleOrderList, getSaleOrderDetail, finishRefund, failureRefund } from '@/api/order'

export default {
  name: 'orderList',
  components: {
    'table-list': table,
    listPagination,
    saleOrderDialog
  },
  mixins: [list],
  extends: {},
  props: {},
  data () {
    return {
      orderNo: '',
      mobile: '',
      inviteMobile: '',
      name: '',
      status: '',
      orderList: [],
      statusList: [
        {
          name: '全部',
          value: ''
        },
        {
          name: '退款中',
          value: 5
        },
        {
          name: '退款成功',
          value: 6
        },
        {
          name: '退款失败',
          value: 7
        }
      ],
      orderDetail: {},
      saleOrderDialogVisible: false,
      timeList: []
    }
  },
  computed: {},
  directives: {},
  filters: {},
  created () {},
  mounted () {
    this.getList()
  },
  watch: {},
  methods: {
    resetSearch () {
      this.orderNo = ''
      this.mobile = ''
      this.inviteMobile = ''
      this.name = ''
      this.status = ''
      this.startTime = ''
      this.endTime = ''
    },
    async getList () {
      let { data } = await getSaleOrderList({
        orderNo: this.orderNo,
        mobile: this.mobile,
        inviteMobile: this.inviteMobile,
        status: this.status,
        page: this.page,
        pageSize: this.pageSize
      })
      this.orderList = data.list
      this.total = data.total
    },
    search () {
      this.page = 1
      this.getList()
    },
    clickToExportOrder () {
      if (this.timeList.length !== 2) {
        this.$message({
          type: 'warning',
          message: '请选择导出日期范围'
        })
        return
      }
      let href = `${process.env.VUE_APP_API}/manage/orders_manage/selledOrderExport?startTime=${this.timeList[0]}&endTime=${this.timeList[1]}&mobile=${this.mobile}&inviteMobile=${this.inviteMobile}&orderNo=${this.orderNo}&status=${this.status}`
      let a = document.createElement('a')
      a.href = href
      a.click()
    },
    async clickToOrderDetail ({ orderNo }) {
      let { data } = await getSaleOrderDetail({
        orderNo
      })
      this.orderDetail = data
      this.saleOrderDialogVisible = true
    },
    clickToResolveRefund ({ orderNo }) {
      this.$confirm('确认完成退款?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await finishRefund({ orderNo })
        this.$message({
          type: 'success',
          message: '完成退款成功!'
        })
        this.getList()
      })
    },
    clickToRejectRefund ({ orderNo }) {
      this.$confirm('确认退款失败?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        await failureRefund({ orderNo })
        this.$message({
          type: 'success',
          message: '退款失败成功!'
        })
        this.getList()
      })
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
